package com.example.javabasic.sample.bootstrap;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * @author: cph
 * @date: 2021/11/11 17:59
 */
@Slf4j
@Component
public class PostconstructTest {

    @Autowired
    private Environment env;

    /**
     * 声明一个Bean对象初始化完成后执行的方法(正常情况下只会执行一次)
     * 被注解的方法不得有任何参数，否则会报如下错误
     * nested exception is java.lang.IllegalStateException: Lifecycle method annotation requires a no-arg method
     */
    @PostConstruct
    public void init() {
        log.info(">>>PostconstructTest#init process...{}", env.getDefaultProfiles()[0]);
    }

    @PreDestroy
    public void preDestory() {
        log.info(">>>PostconstructTest#preDestory process...");
    }

}
